<template>
  <el-dialog v-if="load" :close-on-click-modal="false" :title="form.site_id ? '编辑站点' : '添加站点'" v-model="visible" align-center width="800px" @close="handleClose">
    <el-form ref="formRef" :model="form" label-position="top" :label-width="140">
      <div class="grid grid-cols-1 gap-x-3">
        <el-form-item label="站点名称" prop="site_name" :rules="[{ validator, trigger: 'blur' }]">
          <div class="grid grid-cols-3 w-full">
            <el-input v-model.lazy="form.site_name" placeholder="请输入" :maxlength="128" show-word-limit class="col-span-2" />
            <div class="whitespace-nowrap ml-2">只允许汉字、字母、下划线和数字</div>
          </div>
        </el-form-item>
        <el-form-item label="站点代码" prop="site_code" :rules="[{ validator: validator1, trigger: 'blur' }]">
          <div class="grid grid-cols-3 w-full">
            <el-input v-model.lazy="form.site_code" placeholder="请输入" :disabled="!!form.site_id" :maxlength="64" show-word-limit class="col-span-2" />
            <div class="whitespace-nowrap ml-2">只允许大写字母</div>
          </div>
        </el-form-item>
        <el-form-item label="站点简称" prop="site_abbrev" :rules="[{ validator, trigger: 'blur' }]">
          <div class="grid grid-cols-3 w-full">
            <el-input v-model.lazy="form.site_abbrev" placeholder="请输入" :maxlength="128" show-word-limit class="col-span-2" />
            <div class="whitespace-nowrap ml-2">只允许汉字、字母、下划线和数字</div>
          </div>
        </el-form-item>
        <el-form-item label="站点图标">
          <template #label>
            站点图标
            <span class="text-gray-400 ml-2">
              支持大小：2M 扩展名：.jpg .jpeg .png
            </span>
          </template>
          <ImageUpload v-model="form.site_logo" :limit="1" :disabled="form.shop_id" :fileSize="2" :fileType="['png', 'jpg', 'jpeg']" :isShowTip="false">
          </ImageUpload>
        </el-form-item>
        <div class="grid grid-cols-2 gap-x-3">
          <el-form-item label="站点国家" prop="site_country_code" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
            <VSelect v-model="form.site_country_code" :options="options.site_country" class="w-full" filterable clearable placeholder="请选择" />
          </el-form-item>
          <el-form-item label="语言" prop="language_id" :rules="[{ required: true, message: '不能为空', trigger: 'change' }]">
            <VSelect v-model="form.language_id" :options="options.site_language" class="w-full" filterable clearable placeholder="请选择" />
          </el-form-item>
          <el-form-item label="币种" prop="currency_id">
            <VSelect v-model="form.currency_id" :options="options.currency" class="w-full" filterable clearable placeholder="请选择" />
          </el-form-item>
          <el-form-item label="站点链接">
            <el-input v-model.lazy="form.site_link" placeholder="请输入" :maxlength="200" />
          </el-form-item>
          <el-form-item label="启用状态">
            <el-switch v-model="form.status" inline-prompt active-text="启用" inactive-text="禁用" :active-value='1' :inactive-value='2' />
          </el-form-item>
        </div>
      </div>
    </el-form>
    <template #footer>
      <el-button @click="close">关 闭</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from './api'
import { ElMessage } from 'element-plus'
import ImageUpload from '@/components/ImageUpload'

const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
const success = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

const validator = (rule, value, cb) => {
  if (!value) cb(new Error('不能为空'))
  if (!/^[\u4e00-\u9fa5a-zA-Z0-9_]+$/.test(value)) cb(new Error('只允许汉字、字母、下划线和数字'))
  cb()
}
const validator1 = (rule, value, cb) => {
  if (!value) cb(new Error('不能为空'))
  if (!/^[A-Z]+$/.test(value)) cb(new Error('只允许大写字母'))
  cb()
}

// 打开弹窗
const open = async ({ site_id, platform_id }) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  form.value = { platform_id }
  loadings = reactive({})
  success.value = false
  visible.value = true
  formRef?.value?.clearValidate()
  // 编辑
  if (site_id) {
    const res = await api.detail({ site_id })
    form.value = res.data
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
  success.value && emit('success')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  api[form.value.site_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    success.value = true
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>